﻿namespace CookieTerm
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;

    public class BbsForm : Form
    {
        Bookmark bookmark;
        BbsConsole console;

        public event EventHandler OnStatusChanged;

        public BbsConsole Console
        {
            get
            {
                return this.console;
            }
        }

        public string StatusText
        {
            get 
            { 
                return this.console.StatusText; 
            }
        }

        public BbsForm(Bookmark bookmark)
        {
            this.bookmark = bookmark;
            Text = this.bookmark.Name;

            SuspendLayout();

            BackColor = bookmark.DisplaySettings.Black.ToColor();
            AutoScroll = true;

            this.console = new BbsConsole(bookmark);
            this.console.OnStatusChanged += OnConsoleStatusChanged;

            Controls.Add(this.console);
            ResumeLayout();
        }

        protected override void OnResize(EventArgs e)
        {
            Size diff = ClientSize - this.console.Size;
            int x = Math.Max(diff.Width / 2, 0);
            int y = Math.Max(diff.Height / 2, 0);
            this.console.Location = new Point(x, y);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = !this.console.Controller.RequestToShutdown();
        }

        private void OnConsoleStatusChanged(object sender, EventArgs e)
        {
            if (this.OnStatusChanged != null)
            {
                this.OnStatusChanged(this, e);
            }
        }
    }
}